package com.jsbj.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsbj.tools.RequestHeaderUtils;
import com.jsbj.tools.StringUtils;
import com.jsbj.user.entities.SysFile;
import com.jsbj.user.mapper.SysFileMapper;
import com.jsbj.user.service.ISysFileService;
import io.github.bluemiaomiao.service.FastdfsClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文件表 服务实现类
 * </p>
 *
 * @author jsbj
 * @since 2022-05-17
 */
@Service
public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> implements ISysFileService {

    @Resource
    private FastdfsClientService fastdfsClientService;

    @Autowired
    private HttpServletRequest request;

    @Override
    public Map<String, Object> uploadFileByType(MultipartFile uploadFile, HttpServletRequest request) {
        String userId = RequestHeaderUtils.getUserId(request);

        Map<String, Object> ret = new HashMap<>();
        SysFile configFile = new SysFile();
        configFile.setCreaterId(userId);
        configFile.setUpdateId(userId);
        configFile.setCreateTime(new Date());
        // 文件真实名称
        String realFileName = uploadFile.getOriginalFilename();
        // 文件大小
        String fileSize = uploadFile.getSize() + "";
        // 文件格式
        String fileType = realFileName.substring(realFileName.lastIndexOf("."),realFileName.length());

        try {
            String[] remoteInfo = fastdfsClientService.autoUpload(uploadFile.getBytes(), fileType.substring(1, fileType.length()));
            String groupName = remoteInfo[0];
            String fileName = remoteInfo[1];
            configFile.setRealFileName(realFileName);
            configFile.setFileSize(fileSize);
            configFile.setFileType(fileType);
            configFile.setGroupName(groupName);
            configFile.setFileName(fileName);
            baseMapper.insert(configFile);

            ret.put("groupName", groupName);
            ret.put("fileName", fileName);
            ret.put("fileId", configFile.getId());
            ret.put("fileSize",fileSize);
            ret.put("filePath",getFilePath(configFile.getId()));
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ret;
    }

    @Override
    public SysFile getFilePath(String id) {
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        SysFile sysFile = baseMapper.selectById(id);
        String remoteFile = "";
        if (sysFile == null || StringUtils.isEmpty(sysFile.getGroupName())) {
            return null;
        }
        try {
            remoteFile = fastdfsClientService.autoDownloadWithToken(sysFile.getGroupName(), sysFile.getFileName(), RequestHeaderUtils.getIp(request));
            sysFile.setFileUrl(remoteFile);
        } catch (Exception e) {
            log.error("getFilePath:" + e);
        }

        return sysFile;
    }
}
